package middleware

import (
	"encoding/json"
	"fmt"
	"github.com/kataras/iris/v12"
	"github.com/spf13/cast"
	"time"
	"wit-admin-go/system/app/dto"
	"wit-admin-go/system/utils"
	"wit-admin-go/system/zaplogger"
)

// Before 请求拦截
func Before(ctx iris.Context) {
	t0 := time.Now()
	// 获取请求信息
	var F = new(dto.Log)
	F.Status = 1
	F.Path = ctx.Path()
	ip, _ := utils.GetClientIP(ctx)
	F.IpAddr = ip
	F.Method = ctx.Method()
	F.UserAgent = ctx.Request().UserAgent()
	str, _ := json.Marshal(ctx.FormValues())
	F.Params = cast.ToString(ctx.URLParams())
	F.Data = cast.ToString(str)
	F.RespStatusCode = ctx.GetStatusCode()
	F.Name = ctx.RouteName()
	//fmt.Println("ctx ======>", cast.ToString(ctx.HandlerName()))
	msg := fmt.Sprintf("请求路由：%s  IP：%s  Method：%s  Data：%s  UserAgent：%s", F.Path, F.IpAddr, F.Method, F.Data, F.UserAgent)
	log.Log.Info(msg)
	F.Duration = int(time.Since(t0))
	CheckAuthorization(ctx, F)
}

// After 响应拦截
func After(ctx iris.Context) {
	log.Log.Info("after..........")
	ctx.Next()
}
